<?php
	/*
		PandaForum: PHP Forum Engine
		
		Jesse Gill, 0108377707
	*/
	
	require_once('../classes/DBConnection.php');
	require('../config.php');
	
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	
	$connection = new DBConnection();
	$connection->connect($host, $username, $password, $dbname);
	
	if (! $connection->is_connected())
	{
		die($connection->get_error());
	}
	
	// BEGIN DISPLAY GENERAL FORUMS (without a category)
	$result = $connection->query("SELECT * FROM forum WHERE categoryid=-1 AND parentid=-1 ORDER BY displayorder ASC");
	
	if (! $result)
	{
		die($connection->get_error());
	}
	
	display_category("General", -1, $result, true);
	$result->free();
	// END DISPLAY GENERAL FORUMS (without a category)
	
	// BEGIN DISPLAY CATEGORISED FORUMS
	$result = $connection->query("SELECT * FROM category ORDER BY displayorder ASC");
	
	if (! $result)
	{
		die($connection->get_error());
	}
	
	while ($obj = $result->fetch_object())
	{
		$forums = $connection->query("SELECT * FROM forum WHERE categoryid=$obj->id AND parentid=-1 ORDER BY displayorder ASC");
	
		if (! $result)
		{
			die($connection->get_error());
		}
		
		display_category($obj->name, $obj->id, $forums, false);
		
		$forums->free();
	}
	
	$result->free();
	// END DISPLAY CATEGORISED FORUMS
	
	function display_category($name, $id, $rows, $noncat)
	{
		$i = 1;
?>
		<h3><?php echo $name; ?> <?php if (! $noncat) { echo "(<a href='javascript:ajax_removecategory($id)'>Delete</a>)"; } ?></h3>
<?php
		while ($obj = $rows->fetch_object())
		{
?>
			<div class="foruminfo-<?php echo $i; ?>">
				<div class="forumname"><?php echo $obj->name; ?></div>
				<div class="forumedit">Edit</div>
				<div class="forumdelete"><a href="javascript:ajax_removeforum(<?php echo $obj->id; ?>)">Delete</a></div>
			</div>
<?php
			if ($i == 1)
			{
				$i = 2;
			}
			else
			{
				$i = 1;
			}
		}
	}
	
	$connection->close();
?>
